<?php

namespace App\Admin\Controllers;

use App\Models\PlatSetting;
use App\Models\PlatSettingRemit;

use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;

class PlatSettingRemitController extends Controller
{
    use ModelForm;
    protected $key = 'remit';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('平台出款设置');
            $content->description('');

            $setting = PlatSetting::find($this->key);
            $data = $setting ? $setting->value :[];
            $content->row(function (Row $row) use ($data){

                $form = new Form();
                $digits = 4;

                $form->decimal('remit.threshold', '余额提现条件')
                    ->options(['digits' => $digits])
                    ->help('余额低于此值不能提现')
                    ->rules('required|max:15')
                    ->default(array_key_exists('threshold', $data) ? $data['threshold'] : 0);
                $form->decimal('remit.single_min', '平台单笔最低提款')
                    ->options(['digits' => $digits])
                    ->help('最大14位')
                    ->rules('required|max:15')
                    ->default(array_key_exists('single_min', $data) ? $data['single_min'] : 0);
                $form->decimal('remit.single_max', '平台单笔最大提款')
                    ->options(['digits' => $digits])
                    ->help('最大14位')
                    ->rules('required|max:15')
                    
                    ->default(array_key_exists('single_max', $data) ? $data['single_max'] : 0);
                $form->radio('remit.fee_type', '平台提现手续费类型')->options([
                    0 => '固定',
                    1 => '比例'
                ])
                    
                    ->default(array_key_exists('fee_type', $data) ? $data['fee_type'] : 0);
                $form->decimal('remit.fee_fixed', '固定手续费')
                    ->options(['digits' => $digits])
                    ->help('最大14位')
                    ->default(1)
                    ->rules('required|max:15')
                    
                    ->default(array_key_exists('fee_fixed', $data) ? $data['fee_fixed'] : 0);
                $form->decimal('remit.fee_rate', '手续费比例')
                    ->options(['digits' => 2])
                    ->rules('required_if:fee_type,1|max:5')
                    ->help('当手续费类型为【比例】时生效')
                    
                    ->default(array_key_exists('fee_rate', $data) ? $data['fee_rate'] : 0);
                $form->decimal('remit.fee_max', '最高手续费')
                    ->options(['digits' => $digits])
                    ->rules('required_if:fee_type,0|max:10')
                    ->help('当手续费类型为【比例】时生效')
                    
                    ->default(array_key_exists('fee_max', $data) ? $data['fee_max'] : 0);
                $form->decimal('remit.fee_min', '最低手续费')
                    ->options(['digits' => $digits])
                    ->rules('required_if:fee_type,0|max:8')
                    ->help('当手续费类型为【比例】时生效')
                    
                    ->default(array_key_exists('fee_min', $data) ? $data['fee_min'] : 0);
                $form->decimal('remit.single_min_api', 'api单笔最低提款')
                    ->options(['digits' => $digits])
                    ->help('最大14位')
                    ->rules('required|max:15')
                    
                    ->default(array_key_exists('single_min_api', $data) ? $data['single_min_api'] : 0);
                $form->decimal('remit.single_max_api', 'api单笔最大提款')
                    ->options(['digits' => $digits])
                    ->help('最大14位')
                    ->rules('required|max:15')
                    
                    ->default(array_key_exists('single_max_api', $data) ? $data['single_max_api'] : 0);
                $form->radio('remit.fee_type_api', ' api 手续费类型')->options([
                    0 => '固定',
                    1 => '比例'
                ])
                    ->default(array_key_exists('fee_type_api', $data) ? $data['fee_type_api'] : 0);
                $form->decimal('remit.fee_fixed_api', ' api 固定手续费')
                    ->options(['digits' => $digits])
                    ->help('最大14位')
                    ->default(1)
                    ->rules('required|max:15')
                    
                    ->default(array_key_exists('fee_fixed_api', $data) ? $data['fee_fixed_api'] : 0);

                $form->decimal('remit.fee_rate_api', ' api 手续费比例')
                    ->options(['digits' => $digits])
                    ->rules('required|max:5')
                    ->help('当手续费类型为【比例】时生效')
                    
                    ->default(array_key_exists('fee_rate_api', $data) ? $data['fee_rate_api'] : 0);
                $form->decimal('remit.fee_max_api', ' api 最高手续费')
                    ->options(['digits' => $digits])
                    ->rules('required|max:10')
                    ->help('当手续费类型为【比例】时生效')
                    
                    ->default(array_key_exists('fee_max_api', $data) ? $data['fee_max_api'] : 0);
                $form->decimal('remit.fee_min_api', ' api 最低手续费')
                    ->options(['digits' => $digits])
                    ->rules('required|max:8')
                    ->help('当手续费类型为【比例】时生效')
                    
                    ->default(array_key_exists('fee_min_api', $data) ? $data['fee_min_api'] : 0);

                $form->disablePjax();
                $row->column(12, function (Column $column) use ($form) {
                    $column->append($form);
                });

            });
        });
    }

    public function store(Request $request)
    {
        $setting = $request->input('remit');
        $plat_setting = PlatSetting::find($this->key);
        if ($plat_setting == null) {
            $plat_setting = new PlatSetting();
            $plat_setting->key = $this->key;
        }
        $plat_setting->value = $setting;
        $plat_setting->save();
        return back();
    }

    /**
     * Make a form builder.
     *
     */
    protected function form()
    {
        $digits = 4;

        $tab = new Tab();

        $form = new Form();

        $form->decimal('single_min', '平台单笔最低提款')
            ->options(['digits' => $digits])
            ->help('最大14位')
            ->rules('required|max:15');
        $form->decimal('single_max', '平台单笔最大提款')
            ->options(['digits' => $digits])
            ->help('最大14位')
            ->rules('required|max:15');
        $form->radio('fee_type', '平台提现手续费类型')->options([
            0 => '固定',
            1 => '比例'
        ]);
        $form->decimal('fee_fixed', '固定手续费')
            ->options(['digits' => $digits])
            ->help('最大14位')
            ->default(1)
            ->rules('required|max:15');
        $form->decimal('fee_rate', '手续费比例')
            ->options(['digits' => 2])
            ->rules('required_if:fee_type,1|max:5')
            ->help('当手续费类型为【比例】时生效');
        $form->decimal('fee_max', '最高手续费')
            ->options(['digits' => $digits])
            ->rules('required_if:fee_type,0|max:10')
            ->help('当手续费类型为【比例】时生效');
        $form->decimal('fee_min', '最低手续费')
            ->options(['digits' => $digits])
            ->rules('required_if:fee_type,0|max:8')
            ->help('当手续费类型为【比例】时生效');

        $tab->add('平台提现', $form->render());

        return $tab->render();

    }
}
